package Actions;

import Users.Manager;
import Validator.CustomerValidator;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import db.DBConnector;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Adjell Ian
 */
public class LoginAdmin extends ActionSupport {

    private Manager manager;
    private DBConnector db = null;
    private user user;
    private CustomerValidator valid = new CustomerValidator();
    private String message;

    public LoginAdmin() {
    }

    @Override
    public String execute() throws Exception {
        boolean result = false;
        boolean result2 = false;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        setDb(new DBConnector());
        Connection conn = null;
        boolean specialinpassword = true;
        boolean specialinusername = true;
        user = new user();
        int i = 1;
        String password = "";

        try {
            if ("".equals(getManager().getUsername())) {
                addActionError(getText("username.required"));
                if ("".equals(getManager().getPassword())) {
                    addActionError(getText("password.required"));
                }
                return INPUT;
            }

            specialinpassword = valid.validateuser(getManager().getPassword());
            specialinusername = valid.validateuser(getManager().getUsername());

            if (specialinpassword) {
                addActionError(getText("password.special"));
                if (specialinusername) {
                    addActionError(getText("username.special"));
                }
                return INPUT;
            }
            
            

            conn = getDb().getConnection();

            pstmt = conn.prepareStatement("select username from admin where username=?");

            pstmt.setString(i++, getManager().getUsername());

            rs = pstmt.executeQuery();

            if (rs.next()) {
                result = true;
            } else {
                addActionError(getText("username.invalid"));
            }

            pstmt.clearParameters();

            i = 1;
            pstmt = conn.prepareStatement("select password from admin where username=?");

            pstmt.setString(i++, getManager().getUsername());

            rs = pstmt.executeQuery();
            
            while(rs.next())
            {
                password = rs.getString("password");
            }

            if (getManager().getPassword().equals(password)) {
                result2 = true;
            } else {
                addActionError(getText("password.invalid"));
            }

            conn.close();
        } catch (Exception e) {
            Logger.getLogger(LoginAdmin.class.getName()).log(Level.SEVERE, null, e);
            setMessage(e.getMessage());
            return ERROR;
        }

        getUser().setUsername(getManager().getUsername());
        getUser().setType(2);

        if (result && result2) {
            Map session = ActionContext.getContext().getSession();
            session.put("admin", getUser());
            return "login";
        } else {
            return INPUT;
        }
    }

    /**
     * @return the customer
     */
    public Manager getManager() {
        return manager;
    }

    /**
     * @param customer the customer to set
     */
    public void setManager(Manager manager) {
        this.manager = manager;
    }

    /**
     * @return the db
     */
    public DBConnector getDb() {
        return db;
    }

    /**
     * @param db the db to set
     */
    public void setDb(DBConnector db) {
        this.db = db;
    }

    /**
     * @return the user
     */
    public user getUser() {
        return user;
    }

    /**
     * @param user the user to set
     */
    public void setUser(user user) {
        this.user = user;
    }

    /**
     * @return the message
     */
    public String getMessage() {
        return message;
    }

    /**
     * @param message the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }

    public class user {

        private String username;
        private int type;

        /**
         * @return the username
         */
        public String getUsername() {
            return username;
        }

        /**
         * @param username the username to set
         */
        public void setUsername(String username) {
            this.username = username;
        }

        /**
         * @return the type
         */
        public int getType() {
            return type;
        }

        /**
         * @param type the type to set
         */
        public void setType(int type) {
            this.type = type;
        }
    }
}
